package summary;

/**
 * @Author: 海琳琦
 * @Date: 2022/3/8 19:38
 * https://leetcode-cn.com/problems/swap-nodes-in-pairs/
 */
public class Title24 {

    static class ListNode {
        int val;

        ListNode next;

        public ListNode() {
        }

        public ListNode(int val) {
            this.val = val;
        }

        public ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    public ListNode swapPairs(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode newHead = new ListNode(0);
        newHead.next = head;
        ListNode pre = newHead;
        ListNode cur = head;
        ListNode temp;
        while (cur != null && cur.next != null) {
            temp = cur.next.next;
            pre.next = cur.next;
            cur.next.next = cur;
            cur.next = temp;
            pre = cur;
            cur = temp;
        }
        return newHead.next;
    }

    public ListNode swapPairs1(ListNode head) {
        ListNode newHead = new ListNode(-1);
        newHead.next = head;
        ListNode pre = newHead;
        ListNode cur = head;
        while (cur != null && cur.next != null) {
            ListNode curNext = cur.next.next;
            pre.next = cur.next;
            cur.next.next = cur;
            cur.next = curNext;
            pre = cur;
            cur = cur.next;
        }
        return newHead.next;
    }








    public static void main(String[] args) {
        ListNode head = new ListNode(1);

    }
}
